import { defineStore } from "pinia";
import { Names } from "./store-name";
import { useRequest } from "alova";
import { executeInstall, install } from "@/api/install";
import type { TVersionConfig } from "@/typing/base";


export const useInstallStore = defineStore(Names.INSTALL, {
  state: () => {
    return {
      isInstalled: false,
      versionConfig: <TVersionConfig>{},
      executeLoading: false,
      executeResult: '',
    }
  },
  actions: {
    useInstall() {
      const { onSuccess } = useRequest(install())
      onSuccess(({ data }: any) => {
        this.versionConfig = data.data as TVersionConfig
      })
    },
    execute(req: TExecuteInstallReq, fn: Function) {
      const { onSuccess, loading } = useRequest(executeInstall(req))
      onSuccess(({ data }: any) => {
        const { outPutString } = data.data as { outPutString: string }
        this.executeLoading = loading.value
        this.executeResult = outPutString as string
        fn && fn()
      })
    }

  }

})
